@extends('layouts.scaffold')

@section('head')
	{{ HTML::style('styles/results.css') }}

	<script type="text/javascript">
		var resultsController = function($scope, $http)
		{
			$scope.teams = {{$teams}};
			$scope.showRanking;
			console.log("data:");
				console.log($scope.teams);

			$scope.submitResults = function(){
				

				$http({
				    url: location.origin + location.pathname + "/obter",
				    method: "POST",
				    data: 	{
				    			"teams"	: 	$scope.teams
				    		}
				}).success	(function(data, status, headers, config) {
					console.log("Received data!");
					console.log(data);
					$scope.ranking = data.teams;

					$scope.ranking.sort(function(a, b){
						var g1 = a.ouro;
						var g2 = b.ouro;

						var p1 = a.pontuacao;
						var p2 = b.pontuacao;

						if(!a.terminou || !b.terminou)
						{
							return (a.terminou === b.terminou)? 0 : b.terminou? 1 : -1;
						}
						//se tiverem diferentes qt de ouro
						if (g1 != g2)
						{
							return g2 - g1;
						}
						//se tiverem a mesma qt de ouro
						return p2 - p1;
					});

					$scope.showRanking = true;


				}).error	(function(data, status, headers, config) {
					console.log("Request failed!");
					console.log(data);
				
				});
			}
		}
	</script>
@endsection

@section('navbar')
	@include('menu.navbar')
@endsection

@section('main')
<div ng-controller="resultsController" class="panel panel-primary">
	<div class="panel-primary">
		<h2 class="panel-heading">Resultados do Jogo</h2>
	</div>
	<div class="panel-body">
		<div ng-show="teams.length==0" class="alert alert-danger" role="alert"><strong>ERRO:</strong> O jogo seleccionado não tem equipas criadas!</div>
		<form role="form" ng-valid ng-submit="submitResults()">
			<table class="table">
				<tr>
					<th class="col-md-2"><center>Equipa</center></th>
					<th><center>Terminou</center></th>
					<th><center>Ouro</center></th>
					<th><center>Abastecimentos</center></th>
					<th><center>Combustíveis</center></th>
					<th><center>Pneus</center></th>
					<th><center>Tendas</center></th>
					<th><center>Dinheiro</center></th>
				</tr>
				<tr ng-repeat="team in teams">
					<td ng-bind="team.teamid+'# '+team.nome" ></td>
					<td><center><input type="checkbox" ng-checked="team.terminou" ng-model="team.terminou"></center></td>
					<td><input type="text" class="form-control" min="0" ng-model="team.ouro"></td>
					<td><input type="text" class="form-control" min="0" ng-model="team.abastecimentos"></td>
					<td><input type="text" class="form-control" min="0" ng-model="team.combustiveis"></td>
					<td><input type="text" class="form-control" min="0" ng-model="team.pneus"></td>
					<td><input type="text" class="form-control" min="0" ng-model="team.tendas"></td>
					<td><input type="text" class="form-control" min="0" ng-model="team.dinheiro"></td>
				</tr>
			</table>
			<div class="text-right">
			  	<button type="submit" class="btn btn-primary">Obter Classificação</button>
			</div>
		</form>
	</div>
	<div ng-if="showRanking">
		<div class="panel-primary">
			<h3 class="panel-heading">Classificação do Jogo</h3>
		</div>
		<div ng-repeat="rank in ranking" class="panel-body">
			<p><strong>[[$index+1]]º Lugar: </strong>[[rank.nome]]<span ng-if="rank.terminou"> - lingotes de ouro: [[rank.ouro]] e minérios: [[rank.pontuacao]]</span></p>
		</div>
	</div>
</div>
	

@endsection

@section('scripts')
@endsection